<template>
    <div class="login">
        <div class="login-main">
            <h1>Steam PY市场</h1>
            <div class="ms-login">
                <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="60px">
                    <el-form-item label="账号" prop="name" class="item">
                        <el-input v-model="ruleForm.username" clearable>
                            <i style="color:#0bdf6b;line-height: 50px" slot="prefix" class="el-icon-s-custom"></i>
                        </el-input>
                    </el-form-item>
                    <el-form-item label="密码" prop="password">
                        <el-input v-model="ruleForm.password" show-password clearable>
                            <i style="color:#0bdf6b;line-height: 50px" slot="prefix" class="el-icon-lock"></i>
                        </el-input>
                    </el-form-item>
                  <el-switch
                      class="switch"
                      v-model="loginin"
                      inactive-text="自动登录">
                  </el-switch>
                    <el-form-item>
                        <el-button type="primary" @click="submitForm('ruleForm')" round>登录</el-button>
                        <el-link type="primary" class="register" href="Register">还没账号？立即注册</el-link>
                    </el-form-item>
                </el-form>
            </div>
        </div>
    </div>
</template>

<script>
import {login} from "../api";
export default {
        name: "Login",
        data() {
            return {
                loginin: false,
                ruleForm: {
                    username: '',
                    password: '',
                },
                rules: {
                    username: [
                        {required: true, message: '请输入账号', trigger: 'blur'},
                        {min: 5, max: 13, message: '长度在 5 到 13 个字符', trigger: 'blur'}
                    ],
                    password: [
                        {required: true, message: '请输入密码', trigger: 'blur'},
                        {min: 5, max: 13, message: '长度在 5 到 13 个字符', trigger: 'blur'}
                    ],
                }
            };
        },
        methods: {
            submitForm(formName) {
              const _this = this
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                      login(this.ruleForm).then((res) =>{
                        const token = res.headers['authorization']
                        // //将请求头authorization中得信息共享出去
                        _this.$store.commit('SET_TOKEN', token)
                        // console.log(res.data.data)
                        _this.$store.commit('SET_USERINFO', res.data.data)

                        //将判断是否要进行自动登录，如果要，则将信息存储到本地存储中
                         if(_this.loginin){
                           localStorage.setItem("loginAuto","1")
                         }else {
                           localStorage.setItem("loginAuto","0")
                         }
                        //进行路由跳转
                        _this.$router.push("/Info")
                        this.$message.success("登录成功")
                          }
                      )

                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            }
        },
     created() {
          const token = localStorage.getItem("token");
          const loginAuto = localStorage.getItem("loginAuto");
          if(loginAuto === "1" && token !== ""){
            this.$router.push("/Info")
            this.$message.success("登录成功")
          }
     }
}
</script>

<style scoped>
    .login {
        position: fixed;
        height: 100%;
        width: 100%;
        background-color: #cdeee8;
        text-align: center;
        /*display: inline-block;*/
    }

    .ms-login {
        padding-top: 100px;
        padding-right: 30px;
        font-min-size: 30px;
    }

    .login-main {
        position: absolute;
        left: 50%;
        width: 300px;
        height: 160px;
        margin-left: -190px;
        /*margin-top: -150px;*/
        padding: 40px;
        border-radius: 10px;
        /*background-color: darkred;*/
    }

    >>> .el-form-item__label {
       padding-top: 10px;
        line-height: 30px;
        font-size: 17px;
    }
    /*>>>.el-form-item__label:before{*/
    /*  content: '';*/
    /*}*/
    >>> .el-input {
        font-size: 18px;
    }
    >>>.el-form-item.is-required:not(.is-no-asterisk)>.el-form-item__label:before{
      content: '';
    }
    >>> .el-input__inner {
        border-radius: 10px;
        height: 50px;
        line-height: 50px;
        font-size: 20px;

    }

    >>> .el-button.is-round {
        width: 100%;
    }
    .switch{
      padding-bottom: 20px;
      padding-right: 30px;
    }
</style>
